/**
 * @Description: 修改文件让git有修改
 * @author Beon
 * @date 2021/1/14
*/
import * as fs from 'fs';
import {lzgitJson} from '../../src/utils/lzgitCliFile';
import * as childProcess from 'child_process';

export {
    changeFile,
    commitOnce,
    addRemote
};

/**
 * 改变文件达到文件修改目的
 * @param {String} path 路径
 */
function changeFile (path?: string): void {
    let json;
    try {
        json = JSON.parse(
            fs.readFileSync(path + '/package.json', 'utf8')
        ) as lzgitJson;
    } catch (e) {
        json = {};
    }

    json.test += '1';

    fs.writeFileSync(path + '/package.json', JSON.stringify(json));
}

/**
 * 进行一次提交
 * @param {String} repoPath 路径
 * @returns {Promise<void>}
 */
async function commitOnce(repoPath?: string): Promise<void> {
    changeFile(repoPath);

    await childProcess.execSync('git add .', { cwd: repoPath });

    await childProcess.execSync('git commit -m "添加测试提交信息"', { cwd: repoPath });
}

/**
 * 添加远程库
 * @param {String} repoPath 路径
 * @returns {Promise<void>}
 */
async function addRemote(repoPath?: string): Promise<void> {
    // await childProcess.execSync('git remote add github https://github.com/wyx11223344/LzGitSpecTest/tree/main', { cwd: repoPath });
    await childProcess.execSync('git remote add gitee https://gitee.com/beon/lz-git-spec-test', { cwd: repoPath });
}
